package com.idroid.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.idroid.R;
import com.idroid.fragments.SearchFragment;

public class AddressDialog extends DialogFragment {

	private SearchFragment searchFragment;

	public static AddressDialog newInstance(SearchFragment searchFragment) {

		AddressDialog dialog = new AddressDialog();
		dialog.searchFragment = searchFragment;

		return dialog;
	}

	public View createDialogView() {
		View v = searchFragment.getActivity().getLayoutInflater()
				.inflate(R.layout.address_dialog, null);

		return v;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final View v = createDialogView();

		return new AlertDialog.Builder(getActivity())
				.setTitle("Search by Address")
				.setPositiveButton("Cancel", null).setView(v)
				.setNegativeButton("Ok", new OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						if (which == -2) {
							EditText text = (EditText) v
									.findViewById(R.id.address_text_box);
							boolean fromUsersLocation = ((CheckBox) v
									.findViewById(R.id.fromUserLocation))
									.isChecked();

							searchFragment.startSearchByAddress(text.getText()
									.toString(),fromUsersLocation);
						}

					}
				}).create();
	}

}
